<?php namespace Admin\Controller;
//文章管理
class ArcController extends CommonController{
	private $model;
	public function __auto(){
	    $this->model=new \Common\Model\Arc;
	}
	//添加文章
	public function add(){
	  if(IS_POST){
	  	if($this->model->store()){
	  		View::success('添加成功');
	  	}else{
	  		View::error($this->model->getError());
	  	}
	  }
	  //1处理所属分类
	  $cateModel=new \Common\Model\Cate;
	  $cateData=Data::tree($cateModel->get(),'cname','cid','pid');
//	  p($cateData);
	  View::with('cateData',$cateData);
	  //2处理所属标签
	  $tagModel=new \Common\Model\Tag;
	  View::with('tagData',$tagModel->get());
	  View::make(); 
	}
	//显示文章
	public function index(){
		//1--分页使用
		$page = Page::row(3)->make(Db::table('article')->where("is_recycle=0")->count());
		View::with("page",$page);
		//2.调取数据page::limit是以url传参来区分的
		$data=Db::table('article')->join('category','cid ','=','category_cid')->where('is_recycle=0')->orderBy('sendtime','desc')->field('title,aid,cname,sendtime')->limit(Page::limit())->get();
		View::with('data',$data);
	 	View::make();   
	}
	//删除文章到回收站
	public function delRecycle(){
	  $aid=Q("get.aid");
	  $data=$this->model->where("aid={$aid}")->find();
	  $this->model->where("aid={$aid}")->update(array("is_recycle"=>'1'));
	  View::success('删除文章到回收站成功',U('index'));
	}
	//回收站显示文章
	public function recycle(){
		//统计共有几篇在回收站文章
		$page=Page::row(2)->make(Db::table('article')->where('is_recycle=1')->count());
		View::with('page',$page);
		//让回收站文章有几篇显示出来
		$data=Db::table('article a')->join('category c','a.category_cid','=','c.cid')->where('is_recycle=1')->limit(Page::limit())->get();
		View::with("data",$data);
	    View::make();
	}
	//还原文章
	public function recover(){
		$aid=Q("get.aid",0,'intval');
		$this->model->where("aid={$aid}")->update(array("is_recycle"=>'0'));
	    $this->success('还原成功',U('index'));
	}
	//真正的删除
    public function del(){
    	$aid=Q("get.aid",0,'intval');
        $this->model->where("aid={$aid}")->delete();
		Db::table('article_data')->where("article_aid={$aid}")->delete();
		Db::table('article_tag')->where("article_aid={$aid}")->delete();
    }
	
	
	//编辑文章
	public function edit(){
		if(IS_POST){
			if(!$this->model->edit()) View::error($this->model->getError());
		    View::success("修改成功");
		}
		
		$aid=Q("get.aid",0,'intval');
		//1处理所属分类
		$cateModel=new \Common\Model\Cate;
		$cateData=Data::tree($cateModel->get(),'cname','cid','pid');
		View::with('cateData',$cateData);
		//2处理所属标签
		$tagModel=new \Common\Model\Tag;
		View::with('tagData',$tagModel->get());
		//3获取旧数据
		$oldData=Db::table('article')->join('article_data','article_data.article_aid','=','article.aid')->where("aid={$aid}")->first();
//		p($oldData);
		View::with('oldData',$oldData);
		//当前用户所选择的标签=====>lists是拿出一想要的数据
		$arcTagModel = new \Common\Model\ArcTag;
		$tids=$arcTagModel->where("article_aid={$aid}")->lists('tag_tid');
		//如果用户没有选择标签，那么标签的默认值为NULL，in_array判断就会有问题，所以给默认值为空数组
		$tids = $tids ? $tids : array();
		View::with('tids',$tids);
		View::make();
	}
	//ajax的搜索
	public function search(){
	    if(IS_POST){
	    	$data=$_POST['glod'];
			$data=Db::select("select * from article where title like ?",["%{$data}%"]);
			echo json_encode($data);
	    }
	}
	
}

 ?>
 















